/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(["table", "form", "pkc", "element", "laypage"], function (e) {
  var t = layui.$,
    i = layui.table,
    r = (layui.form, layui.element),
    layform = layui.form,
    laypage = layui.laypage,
    pkc = layui.pkc;
  var form = {
    local: {
      fm: null //当前页面form元素
    },
    /*
     * 初始化表格*
     * */
    initGrid: function () {
      i.init('LAY-app-parkinfo', {
        elem: "#LAY-app-parkinfo",
        id: "LAY-app-parkinfo",
        //cellMinWidth: 80,
        cols: [
          [{
              type: "numbers",
              fixed: "left"
            },
            {
              type: "checkbox"
            },
            {
              field: "username",
              width: 200,
              title: "用户名",
              align: "center"

            },
            {
              field: "userdisplayname",
              width: 200,
              title: "显示名",
              align: "center"

            },
            {
              field: "rolename",
              width: 200,
              title: "角色",
              align: "center"

            },
            {
              field: "enabled",
              width: 200,
              title: "是否启用",
              align: "center",
              templet:"#statusTpl"

            },
            {
              field: "remark",
              width: 200,
              title: "备注",
              align: "center"

            },
            {
              title:"操作",
              width: 320,
              align:"center",
              fixed: "right",
              templet: "#table-system-order"
            }
          ]
        ],
        toolbar: "#toolbar",
        text: {
          none: "暂无相关数据"
        },
        done: (res, curr, count) => {
          // r.init();
          t(window).resize();
          r.render("progress");
          count || t('div.layui-table-box>div').css('overflow', 'auto');
        }
      });


    },
    /**
     * 初始化分页
     * @param {number} count 总数
     * @param {number} pindex 当前页
     */
    initPage: function (count, pindex) {
      laypage.render({
        elem: "apppage",
        count: count,
        layout: ['limit', 'prev', 'page', 'next', 'count', 'refresh', 'skip'],
        jump: function (obj, first) {
          //obj包含了当前分页的所有参数，比如：
          // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
          // console.log(obj.limit); //得到每页显示的条数

          //首次不执行
          if (!first) {
            //do something
            form.reload(obj.curr, obj.limit);
          }
        },
        curr: pindex
      });
    },
    /**
     * 初始化控件事件
     * */
    initEv: function () {
      layform.on("submit(LAY-app-order-search)", function (e) {
        form.local.fm = e;
        form.reload(1, 10);
      });
      i.on("tool(LAY-app-parkinfo)", function (e) {
        var data = e.data;
        var id = e.data.id;
        if ("edit" === e.event) {
          t(e.tr);
          layer.open({
            type: 2,
            title: "编辑用户权限",
            content: "usersetedit.html",
            area: ["800px", "500px"],
            yes: function (e, t) {
            },
            success: function (e, t) {
              window.data = data;
              window.reloadGrid = form.reload;
            }
          });
        }else if("editpsw" === e.event) {
          t(e.tr);
          layer.open({
            type: 2,
            title: "修改用户密码",
            content: "userseteditpsw.html",
            area: ["500px", "500px"],
            yes: function (e, t) {
            },
            success: function (e, t) {
              window.data = data;
              window.reloadGrid = form.reload;
            }
          });
        }else if("del" === e.event){
          layer.confirm("确定需要删除该数据?", {
            icon: 3,
            title: "提示"
          }, function (index) {
              form.toolAction.del(id).done(() => {
                  layer.close(index);
              });
          });

        }
      });
      i.on("toolbar(LAY-app-parkinfo)", function (e) {
        e.data;
        if ("update" === e.event) {
          form.toolAction.edit();
        } else if ("add" === e.event) {
          form.toolAction.add();
        } else if ("delete" === e.event) {
          layer.alert("你正在删除数据");
        }
      });
    },
    /**
     * 界面初始化
     * */
    init: function () {
      this.initGrid();
      this.initPage();
      this.initEv();      
    },
    /**
     * 重载表格
     * @param {number} p 页码
     * @param {number} l 每页的数量
     */
    reload: function (p, l) {
      layer.load("1");
      var data = {};
      // form.getWebParam(form.local.fm);
      data["page"] = p;
      data["rows"] = l;
      pkc.http
        .SendPost(
          data,
          "cs.c01.get",
          function (ret) {
            if (ret) {
              ret = JSON.parse(ret);
              if (ret !== null && ret.Status === "1") {
                //加载表格
                i.reload("LAY-app-parkinfo", {
                  data: ret.Data
                });
                i.resize("LAY-app-parkinfo");
              } else {
                // 虚假数据
                i.reload("LAY-app-parkinfo",{
                  data:[{id:"0001",gateid:"11",lanename:"门岗0001号",no:"0001"}]
                })                
                // layer.alert("网络异常.请稍后再试！");
              }
            }
          },
          function (res) {
            layer.alert("网络异常.请稍后再试！");
            // layer.closeAll("loading");
          }
        )
        .always(function () {
          setTimeout(() => {
            layer.closeAll("loading");
          }, 1000);
        });
    },

    toolAction: {
      /**
       * 新增
       * */
      add: function () {
        layer.open({
          id: "add",
          type: 2,
          title: "新增用户权限",
          content: "usersetadd.html",
          area: ["600px", "500px"],
          yes: function (e, t) {
          },
          success: function (layero, i) {
            window.reloadGrid = form.reload;
          }
        });
      },
      del: function (id) {
        layer.load("1");
          var data = {}; //form.getWebParam(form.local.fm);
          data.id = id;
          //data["pageindex"] = p;
          //data["pagesize"] = l;
          return pkc.http
              .SendPost(
                  data,
                  "cs.c01.delete",
                  function (ret) {
                      if (ret) {
                          ret = JSON.parse(ret);
                          if (ret !== null && ret.Status === "1") {
                              layer.msg('操作成功');
                              form.reload(1, 10);
                          } else {
                              layer.alert("网络异常.请稍后再试！");
                          }
                      }
                  },
                  function (res) {
                      layer.alert("网络异常.请稍后再试！");
                      // layer.closeAll("loading");
                  }
              )
              .always(function () {
                  setTimeout(() => {
                      layer.closeAll("loading");
                  }, 1500);
              });

        },
    }
  };
  e("userset", {
    init: function(){
        form.init();
    },
    loadData: function () {
      form.reload(1, 10);
    }
  });
});